package com.ywq.train;

import com.ywq.train.service.OrderService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@ComponentScan("com.ywq.train")
@EnableAspectJAutoProxy
@EnableTransactionManagement
@Configuration
public class AppConfig {

    @Bean
    public OrderService orderService1(){
        return new OrderService();
    }

    @Bean
    public OrderService orderService2(){
        return new OrderService();
    }

/*_______________________________事务___________________________________________*/


    /* 保证下面两个dataSource是同一个，才可以进行自定义事务的操作,就需要在类名上加一个@Configuration注解,
    原理是通过AppConfig的代理对象执行,会检测该类下面是否有dataSource方法 有则调用*/
    @Bean
    //相当于mybatis
    public JdbcTemplate jdbcTemplate(){
        return new JdbcTemplate(dataSource());
    }

    @Bean
    //事务管理器
    public PlatformTransactionManager transactionManager(){
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource());
        return transactionManager;
    }

    @Bean
    public DataSource dataSource(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUrl("jdbc:mysql://127.0.0.1:65535/springdb?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");
        return dataSource;
    }









}
